<?
  function getHash(){
    $ch = curl_init();
    $url = 'http://s4.sfgame.us/request.php?req=00000000000000000000000000000000002Aptik2%3Bd797529d7da2ff307a73da308ae6ae17%3Bv1.60&random=%2&rnd='.rand(100000000, 999999999);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $character_data = explode('/',curl_exec($ch));
    $hash = explode(';', $character_data[511]);
    $_SESSION['hash'] = $hash[2];
    curl_close($ch);
    header('Location: http://'.$_SERVER['HTTP_HOST'].rtrim($_SERVER['REQUEST_URI'], '/\\'));
  }

  function setCurrentCharacter(){
    if(isset($_POST['character']) && $_POST['character'] != ''){
      $_SESSION["character"] =  $_POST['character'];
      header('Location: http://'.$_SERVER['HTTP_HOST'].rtrim($_SERVER['REQUEST_URI'], '/\\'));
      exit();
    }else{
      return $_SESSION["character"];
    }
  }

  function getCharacterData($name){
    global $_script_debug;
    $ch = curl_init();
    $url = 'http://s4.sfgame.us/request.php?req='.$_SESSION['hash'].'513'.urlencode($name).'&random=%2&rnd='.rand(100000000, 999999999);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $character_data = explode('/',curl_exec($ch));
    curl_close($ch);
    return $character_data;
  }

  function getOponents($character_data, $run = 2, $top = 1){
    global $_script_debug;
    $ch = curl_init();
    for($i=0;$i<$run;$i++){
      if($top == 1) $get = $character_data[11]-8-($i*15);
      else $get  = $character_data[11]+8+($i*15);
      $url = "http://s4.sfgame.us/request.php?req=".$_SESSION['hash'].'007%3B'.$get."&random=%2&rnd=".rand(100000000, 999999999);
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      $result_tmp = ltrim(curl_exec($ch),'+');
      echo ($_script_debug==1)?'<hr>'.$result_tmp.'<hr>':'';
      if($top == 1) $result = substr($result_tmp, 3,-1).$result;
      else $result .= substr($result_tmp, 3,-1);
    }

    curl_close($ch);
    return explode('/',$result);
  }
  
  function getClassName($val){
    switch($val){
      case 1: return 'Warior'; break;
      case 3: return 'Ranger'; break;
      case 2: return 'Mage'; break;
    }
  }
  
  function getMainStat($character_data){
    switch($character_data[29]){
      case 1: return $character_data[35]+$character_data[30]; break;
      case 3: return $character_data[36]+$character_data[31]; break;
      case 2: return $character_data[37]+$character_data[32]; break;
    }
  }
  
  function getHPCoficient($val){
    switch($val){
      case 1: return 5; break;
      case 3: return 4; break;
      case 2: return 2; break;
    }
  }
  
  function getRes($character_data, $val){
    switch($val){
      case 1: return floor(($character_data[35]+$character_data[30])/2); break;
      case 3: return floor(($character_data[36]+$character_data[31])/2); break;
      case 2: return floor(($character_data[37]+$character_data[32])/2); break;
    }
  }
  
  function getHP($character_data){
    $k = 1;
    if($character_data[493] == '16' && $character_data[499] == 25) $k = 1.25;
    if($character_data[494] == '16' && $character_data[500] == 25) $k = 1.25;
    if($character_data[495] == '16' && $character_data[501] == 25) $k = 1.25;
    return round(($character_data[38]+$character_data[33])*getHPCoficient($character_data[29])*($character_data[7]+1))*$k;
  }
  
  function getDPS($character_data, $oponent_data = array()){
    $oponent_res = 0;
    if(sizeof($oponent_data)) $oponent_res = getRes($oponent_data, $character_data[29]);
    return round(($character_data[146]*(1+(getMainStat($character_data)-$oponent_res)/10)+$character_data[147]*(1+(getMainStat($character_data)-$oponent_res)/10))/2);
  }
  
  function getArmor($character_data){
    return floor($character_data[447]/$character_data[7]);
  }
  
  function getCrit($character_data, $lvl = 0){
    if($lvl > 0) return round((($character_data[39]+$character_data[34])*5)/($lvl*2),2);
    else return round((($character_data[39]+$character_data[34])*5)/($character_data[7]*2), 2);
  }

  function getArmorBonus($character, $oponent){
    switch($oponent[29]){
      case 1: return 1-getArmor($character)/100; break;
      case 3: return 1-getArmor($character)/125; break;
      case 2: return 1; break;
    }
  }
  
  function calculateResultsFormula($character, $oponent){
    $rounds = ceil(getHP($character)/getDPS($oponent));
    return getHP($character)."/(".getDPS($oponent)."*".getArmorBonus($character, $oponent).")-".
      getHP($oponent)."/(".getDPS($character)."*".getArmorBonus($oponent, $character).")+".
      "((".getCrit($character, $oponent[7])." - ".getCrit($oponent, $character[7]).")/100)*".$rounds ."+".
      "((".getRes($character, $oponent[29])." - ".getRes($oponent, $character[29]).")/100)*".$rounds;
  }
  
  function calculateResults($character, $oponent){
    $rounds = ceil(getHP($character)/getDPS($oponent));
    $fomula = 1; //current formula number
    switch ($fomula) {
        case 1: return round(
                            getHP($character) / (getDPS($oponent,$character) * getArmorBonus($character, $oponent)) -
                            getHP($oponent) / (getDPS($character,$oponent) * getArmorBonus($oponent, $character)) +
                            ((getCrit($character, $oponent[7]) - getCrit($oponent, $character[7])) / 100) * $rounds +
                            0 //$rounds*(1.2)*((round($character[146]*(1+getMainStat($character)/10))/round($oponent[146]*(1+getMainStat($oponent)/10)))-1)
                            , 3);
            break;
        case 2: return round(getHP($character) / (getDPS($oponent) * (1 + getCrit($oponent, $character[7]) / 100) * getArmorBonus($character, $oponent))
                            - getHP($oponent) / (getDPS($character) * (1 + getCrit($character, $oponent[7]) / 100) * getArmorBonus($oponent, $character)), 3);
            break;
        default: return round(getHP($character) / (getDPS($oponent) * (1 + getCrit($oponent, $character[7]) / 100) * getArmorBonus($character, $oponent))
                            - getHP($oponent) / (getDPS($character) * (1 + getCrit($character, $oponent[7]) / 100) * getArmorBonus($oponent, $character)), 3);
            break;
    }

  }
?>
